import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:incidentmanager/blocs/authentication/authentication_bloc.dart';
import 'package:incidentmanager/blocs/case/case_bloc.dart';
import 'package:incidentmanager/common/data/api/api_client.dart';
import 'package:incidentmanager/common/data/repositories/authentication_repository.dart';
import 'package:incidentmanager/common/data/repositories/user_repository.dart';
import 'package:incidentmanager/common/resource/colors.dart';
import 'package:incidentmanager/ui/home/home_page.dart';
import 'package:incidentmanager/ui/login/login_page.dart';
import 'package:incidentmanager/ui/splash/splash_page.dart';

class App extends StatelessWidget {
  const App({
    Key key,
    @required this.authenticationRepository,
    @required this.userRepository,
    @required this.httpClient,
    @required this.apiClient,
  })  : assert(authenticationRepository != null),
        assert(userRepository != null),
        super(key: key);

  final AuthenticationRepository authenticationRepository;
  final UserRepository userRepository;
  final Dio httpClient;
  final ApiClient apiClient;

  @override
  Widget build(BuildContext context) {
    return RepositoryProvider.value(
      value: authenticationRepository,
      child: MultiBlocProvider(
        providers: [
          BlocProvider<AuthenticationBloc>(
            create: (_) => AuthenticationBloc(
              authenticationRepository: authenticationRepository,
              userRepository: userRepository,
            ),
          ),
          BlocProvider<CaseBloc>(
            create: (_) => CaseBloc(
              httpClient: httpClient,
            ),
          ),
        ],
        child: AppView(),
      ),
    );
  }
}

class AppView extends StatefulWidget {
  @override
  _AppViewState createState() => _AppViewState();
}

class _AppViewState extends State<AppView> {
  final _navigatorKey = GlobalKey<NavigatorState>();

  NavigatorState get _navigator => _navigatorKey.currentState;

  @override
  Widget build(BuildContext context) {
    return FlutterEasyLoading(
      child: MaterialApp(
        navigatorKey: _navigatorKey,
        theme: ThemeData(
          primaryColor: AppColors.PRIMARY,
          visualDensity: VisualDensity.adaptivePlatformDensity,
          backgroundColor: AppColors.BACKGROUND,
        ),
        builder: (context, child) {
          return BlocListener<AuthenticationBloc, AuthenticationState>(
            listener: (context, state) {
              switch (state.status) {
                case AuthenticationStatus.authenticated:
                  _navigator.pushAndRemoveUntil<void>(
                    HomePage.route(),
                    (route) => false,
                  );
                  break;
                case AuthenticationStatus.unauthenticated:
                  _navigator.pushAndRemoveUntil<void>(
                    LoginPage.route(),
                    (route) => false,
                  );
                  break;
                default:
                  break;
              }
            },
            child: child,
          );
        },
        onGenerateRoute: (_) => SplashPage.route(),
      ),
    );
  }
}
